<template>
  <div class="login-papg">
    <div class="login-box">
      <!-- 表单 -->
      <!-- :rules='表单的' -->
      <el-form
        ref="loginForm"
        class="login_form"
        :rules="loginFormRules"
        :model="loginForm"
      >
        <el-form-item prop="username">
          <!-- 用户名输入框 -->
          <el-input
            v-model="loginForm.username"
            prefix-icon="iconfont icon-user"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <!-- 密码输入框 -->
          <el-input
            v-model="loginForm.password"
            prefix-icon="iconfont icon-3702mima"
            type="password"
          ></el-input>
        </el-form-item>
        <!-- 登录，重置按钮 -->
        <el-form-item class="btns">
          <el-button type="primary" size="medium" round @click="login"
            >登陆</el-button
          >
          <el-button type="danger" size="medium" round @click="resetForm"
            >重置</el-button
          >
        </el-form-item>
      </el-form>
      <!-- 头像 -->
      <div class="avatar">
        <img src="../assets/1.jpg" alt="" />
      </div>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 输入框的数据绑定
      loginForm: {
        username: 'admin',
        password: '123456'
      },
      // 表单设置校验
      loginFormRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 7, message: '长度在 2 到 7 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 2, max: 7, message: '长度在 2 到 7 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    // 点击按钮重置表单
    resetForm() {
      this.$refs.loginForm.resetFields()
    },
    login() {
      this.$refs.loginForm.validate(async flag => {
        if (!flag) return
        const { data: res } = await this.$axios.post('/login', this.loginForm)
        if (res.meta.status !== 200) {
          return this.$message.error('用户名或密码不正确')
        }
        this.$message.success('登录成功')
        sessionStorage.setItem('token', res.data.token)
        this.$router.push('/home')
      })
    }
  },

  created() {}
}
</script>

<style scoped lang='less'>
.login-papg {
  width: 100%;
  height: 100%;
  background: #087ff6;
  display: flex;
  justify-content: center;
  align-items: center;
}
.login-box {
  width: 450px;
  height: 300px;
  background-color: rgb(252, 248, 9);
  border-radius: 30px;
  position: relative;
}
.avatar {
  position: absolute;
  top: -50px;
  left: 50%;
  transform: translateX(-50%);
  width: 130px;
  height: 130px;
  background-color: #fff;
  border-radius: 50%;
  box-shadow: 0px 0px 10px yellow;
  display: flex;
  justify-content: center;
  align-items: center;
}
img {
  width: 125px;
  height: 125px;
  border-radius: 50%;
}
.login_form {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0 15px;
  box-sizing: border-box;
  .btns {
    float: right;
  }
}
</style>
